<?php
class UserController extends Controller
{
	private $themes = array();
	public function actionIndex()
	{
		Yii::app()->getModule('user');
		$dataProvider=new CActiveDataProvider('User');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}
	public function actionSetting(){
		Yii::app()->getModule('user');
		$model=new UserSettingForm;
		
		if(isset($_POST)){
			//print_r($_POST);
			//print_r($model->attributes);
			//print_r($model);
			//die();
		}
		
		$this->render('setting',array(
			'model'=>$model,
		));
	}
	public function actionPrivilege(){
		//$menuData['parents']['root'] = array('admin');
		//die(print_r($menuData));
		//die($this->buildMenu('root', $menuData));
		$this->render('privilege',array());
	}
	public function actionProfile(){
		Yii::app()->getModule('user');
		$dataProvider=new CActiveDataProvider('ProfileField');
		$this->render('profile',array(
			'dataProvider'=>$dataProvider,
		));
		//$menuData['parents']['root'] = array('admin');
		//die(print_r($menuData));
		//die($this->buildMenu('root', $menuData));
	}
	public function actionCreateProfileField(){
		Yii::app()->getModule('user');
		$model = new ProfileField;
		$profile = new UserProfile;
		//$menuData['parents']['root'] = array('admin');
		//die(print_r($menuData));
		//die($this->buildMenu('root', $menuData));
		
		if(isset($_POST['ProfileField']))
		{
			$model->attributes=$_POST['ProfileField'];
			$model->dataLength = $_POST['ProfileField']['dataLength'];
			$model->dataType = $_POST['ProfileField']['dataType'];
			if(!empty($_POST['ProfileField']['options'])){
			$model->options = $_POST['ProfileField']['options'];
			}
			if(!empty($_POST['ProfileField']['default'])){
			$model->default = $_POST['ProfileField']['default'];
			}
			
			if(isset($_POST['ajax']) && $_POST['ajax']==='profile-field-form')
			{
				echo CActiveForm::validate($model);
				Yii::app()->end();
			}
			
			if($model->validate()){
				//die(print_r($model));
				$dataType = $model->dataType;
				if(isset($model->dataLength)){
					$dataType .= '('.$model->dataLength.')';
				}
				$transaction=Yii::app()->db->beginTransaction();
				
				try{
				    //die('ALTER TABLE `'.$model->tableName().'` ADD `'.$model->name.'` '.$dataType);
					$command=Yii::app()->db->createCommand('ALTER TABLE `'.$profile->tableName().'` ADD `'.$model->name.'` '.$dataType);
					$command->execute();
					if($model->save(false)){
						$transaction->commit();
					    $this->redirect(array('profile'));
					    exit;
					}
					
				}
				catch(CDbException $e)
				{
				    if($e->getCode() == 42){
						$model->addError('name',Yii::t('admin_user_profile','The field name has already exists, please try another field.'));
				    }else if($e->getCode() == 42000){
				    	$model->addError('dataLength',Yii::t('admin_user_profile','The data length is not valid.'));
				    }else{
				    	$model->addError('name',$e->getMessage());
				    }
					$transaction->rollBack();
				}
			}
		}
		
		$this->render('add_profile_field',array('model'=>$model));
	}
	public function actionDeleteProfileField(){
		Yii::app()->getModule('user');
		$profileField = new ProfileField;
		$userProfile = new UserProfile;
		$profile = new UserProfile;
		
		$transaction=Yii::app()->db->beginTransaction();
		try{
			$command=Yii::app()->db->createCommand('ALTER TABLE `'.$profile->tableName().'` DROP `'.$_GET['name'].'` ');
			$command->execute();
			
			$profileField->deleteByPk($_GET['name']);
			
			$transaction->commit();
		}catch (Exception $e){
			$transaction->rollBack();					
		}
	}
	public function actionEditProfileField(){
		Yii::app()->getModule('user');
		$model = ProfileField::model()->findByPk($_GET['name']);
		$profile = new UserProfile;
		//$model = $model->findByPk($_GET['name']);
		//print_r($editedField);
		
		if(isset($_POST['ProfileField']))
			{
				$model->attributes=$_POST['ProfileField'];
				$model->dataLength = $_POST['ProfileField']['dataLength'];
				$model->dataType = $_POST['ProfileField']['dataType'];
				if(!empty($_POST['ProfileField']['options'])){
				$model->options = $_POST['ProfileField']['options'];
				}
				if(!empty($_POST['ProfileField']['default'])){
				$model->default = $_POST['ProfileField']['default'];
				}
				
				if(isset($_POST['ajax']) && $_POST['ajax']==='profile-field-form')
				{
					echo CActiveForm::validate($model);
					Yii::app()->end();
				}
				
				if($model->validate()){
					//die(print_r($model));
					$dataType = $model->dataType;
					if(isset($model->dataLength)){
						$dataType .= '('.$model->dataLength.')';
					}
					$transaction=Yii::app()->db->beginTransaction();
					
					try{
					    //die('ALTER TABLE `'.$model->tableName().'` ADD `'.$model->name.'` '.$dataType);
						$command=Yii::app()->db->createCommand('ALTER TABLE `'.$profile->tableName().'` MODIFY `'.$model->name.'` '.$dataType);
						$command->execute();
						if($model->save(false)){
							$transaction->commit();
						    $this->redirect(array('profile'));
						    exit;
						}
						
					}
					catch(CDbException $e)
					{
					    if($e->getCode() == 42){
							$model->addError('name',Yii::t('admin_user_profile','The field name has already exists, please try another field.'));
					    }else if($e->getCode() == 42000){
					    	$model->addError('dataLength',Yii::t('admin_user_profile','The data length is not valid.'));
					    }else{
					    	$model->addError('name',$e->getMessage());
					    }
						$transaction->rollBack();
					}
				}
			}
		
		
		$this->render('add_profile_field',array('model'=>$model));
	}
	public function actionCreate(){
		Yii::app()->getModule('user');
		//Yii::app()->email->send('hau.danh@swissitbridge.com','Test email', 'Tessst');
		//print_r(Yii::app()->email);
		$model = new User;
		$model->setScenario('admin_create');
		$profileField = new ProfileField;
		$userProfile = new UserProfile;
		$model->setScenario('admin_create');
		if(isset($_POST['User']))
		{
			$model->attributes=$_POST['User'];
			$userProfile->attributes = $_POST['UserProfile'];
			//print_r($userProfile->rules());
			//die('asdasd');
			if($model->validate('admin_create') && $userProfile->validate()){
				$transaction=Yii::app()->db->beginTransaction();
				try{
					if($model->save(false)){
						$userProfile->user_id = $model->id;
						$userProfile->save(false);
						$transaction->commit();
						$this->redirect(array('admin/user'));
						exit;
					}
				}catch (Exception $e){
					$model->addError('username', $e->getMessage());
					$transaction->rollBack();					
				}
			}
		}
		
		$this->render('create',array('model'=>$model,'profileField'=>$profileField,'userProfile'=>$userProfile));
	}
	public function actionNewRole(){
		Yii::app()->getModule('user');
		$model = new RoleForm;
		
		if(isset($_POST['RoleForm']))
		{
			$model->attributes=$_POST['RoleForm'];
			try{
				$role = Yii::app()->authManager->createRole($model->role,$model->description,$model->bizrule);
			}catch (Exception $e){
				$model->addError('role', $e->getMessage());				
			}
		}
		
		$this->render('add_role',array('model'=>$model));
	}
	public function actionNewTask(){
		Yii::app()->getModule('user');
		$model = new TaskForm;
		
		if(isset($_POST['TaskForm']))
		{
			$model->attributes=$_POST['TaskForm'];
			try{
				$role = Yii::app()->authManager->createTask($model->task,$model->description,$model->bizrule);
			}catch (Exception $e){
				$model->addError('role', $e->getMessage());				
			}
		}
		
		$this->render('add_task',array('model'=>$model));
		
	}
}